﻿using MarkdownTable;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Windows;
using System.Windows.Controls;

namespace Json2MdTableExtension2022
{
    /// <summary>
    /// Interaction logic for Json2MdTableControl.
    /// </summary>
    public partial class Json2MdTableControl : UserControl
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Json2MdTableControl"/> class.
        /// </summary>
        public Json2MdTableControl()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Handles click on the button by displaying a message box.
        /// </summary>
        /// <param name="sender">The event sender.</param>
        /// <param name="e">The event args.</param>
        [SuppressMessage("Microsoft.Globalization", "CA1300:SpecifyMessageBoxOptions", Justification = "Sample code")]
        [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1300:ElementMustBeginWithUpperCaseLetter", Justification = "Default event handler naming pattern")]
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(
                string.Format(System.Globalization.CultureInfo.CurrentUICulture, "Invoked '{0}'", this.ToString()),
                "Json2MdTable");
        }

        private void TransButton_Click(object sender, RoutedEventArgs e)
        {
            string jsonStr = ContentTextBox.Text;
            if (string.IsNullOrEmpty(jsonStr))
                MessageBox.Show("输入为空");

            dynamic jsonObjList = null;
            try
            {
                jsonObjList = JsonConvert.DeserializeObject<dynamic>(jsonStr);
            }
            catch (Exception ex)
            {
                MessageBox.Show("输入格式错误，" + ex.Message);
            }
            if (jsonObjList != null)
            {
                Table table = new Table();
                table.Headers = new List<Column>()
                {
                    new Column()
                    {
                        Name = "参数"
                    },
                    new Column()
                    {
                        Name = "类型"
                    },
                    new Column()
                    {
                        Name = "是否必填"
                    },
                    new Column()
                    {
                        Name = "描述"
                    },
                    new Column()
                    {
                        Name = "示例"
                    }
                };
                var rows = new List<Row>();
                foreach (var jsonObj in jsonObjList)
                {
                    var name = jsonObj.Name;
                    var value = jsonObj.Value.ToString().Replace("\n", "").Replace("\t", "").Replace("\r", "").Replace(" ", "");

                    var row = new Row()
                    {
                        Cells = new List<string>()
                        {
                            name,
                            "string",
                            "否",
                            "",
                            value
                        }
                    };
                    rows.Add(row);
                }
                table.Rows = rows;

                string generatedTable = MarkdownTableGenerator.GenerateTable(table);
                ContentTextBox.Text = generatedTable;
            }
        }
    }
}